<?php
/**
 * Handles account creation for rebuilding user database
 *
 * @author Chris Lunsford
 */
class Beta extends Controller {
	
	/**
	 * Class constructor
	 */
	function __construct() {
		parent::__construct();
		
		$this->load->model('UserModel');		
		$this->load->library('BadgerController');	
	}
	
	function signup() {
		$this->load->library('validation');
		
		$rules['username'] = "trim|required|alpha_numeric|min_length[5]|max_length[50]|callback_username_unique|xss_clean";
		$rules['password'] = "trim|required|min_length[5]|max_length[40]|matches[conf_password]";
		$rules['conf_password'] = "trim";
		$rules['email'] = "trim|required|valid_email";
		$this->validation->set_rules($rules);
		
		$fields['username']	= 'Username';
		$fields['password']	= 'Password';
		$fields['conf_password']	= 'Confirm Password';
		$fields['email']	= 'Email Address';
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		
		if($this->validation->run() == FALSE) {
			$this->load->view('beta/signup');
		} else {
			$post['username'] = $this->input->post('username',TRUE);
			$post['password'] = $this->input->post('password',TRUE);
			$post['email'] = $this->input->post('email',TRUE);
			$this->UserModel->createUser($post);
			$this->load->view('beta/success');
		}
	}
	
	function username_unique($str) {
		if ($this->UserModel->isUsername($str)) {
			$this->validation->set_message('username_unique', 'This username already exists.  Please choose a different username.');
			return FALSE;
		} else {
			return TRUE;
		}
	}
}
?>